package com.moniteach;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.moniteach.constaints.C;

import android.content.Context;
import android.content.SharedPreferences;

public class Prefs {
	private final String PREFS_NAME = "User";
	private Long id = (long) -1;
	private String email = "";
	private String pass = "";
	private String fName = "";
	private String lName = "";
	private String sName = "";
	private String phone = "";
	private String role = "";
	private String pincode = "";
	private String pincodenew = "";
	private Boolean active = false;

	private SharedPreferences settings;
	private Context context;
	private static Prefs instance;

	private Prefs() {
		if (this.context != null) {
			this.id = settings.getLong(C.Const.id, (Long) null);
			this.email = settings.getString(C.Const.email, "");
			this.pass = settings.getString(C.Const.pass, "");
			this.fName = settings.getString(C.Const.fName, "");
			this.lName = settings.getString(C.Const.lName, "");
			this.sName = settings.getString(C.Const.sName, "");
			this.phone = settings.getString(C.Const.phone, "");
			this.role = settings.getString(C.Const.role, "");
			this.pincode = settings.getString(C.Const.pincode, "");
			this.active = settings.getBoolean(C.Const.active, false);
		}
	}

	public synchronized static Prefs getInstance() {
		if (instance == null) {
			instance = new Prefs();
		}
		return instance;
	}

	public void clear() {
		this.id = (long) -1;
		this.email = "";
		this.pass = "";
		this.fName = "";
		this.lName = "";
		this.sName = "";
		this.phone = "";
		this.role = "";
		this.pincode = "";
		this.active = false;
		commit();
	}

	public String getUserToString() {
		return String.format(C.Url.adduser, email, pass, role, fName,
				lName, sName, phone, pincode, "", "", active);
	}

	public List<NameValuePair> toNameValuePairs() {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair(C.Const.action,
				C.Action.MODIFY));
		nameValuePairs.add(new BasicNameValuePair(C.Const.id, String
				.valueOf(id)));
		nameValuePairs
				.add(new BasicNameValuePair(C.Const.email, email));
		nameValuePairs.add(new BasicNameValuePair(C.Const.pass, pass));
		nameValuePairs
				.add(new BasicNameValuePair(C.Const.fName, fName));
		nameValuePairs
				.add(new BasicNameValuePair(C.Const.lName, lName));
		nameValuePairs
				.add(new BasicNameValuePair(C.Const.sName, sName));
		nameValuePairs
				.add(new BasicNameValuePair(C.Const.phone, phone));
		nameValuePairs.add(new BasicNameValuePair(C.Const.role, role));
		nameValuePairs.add(new BasicNameValuePair(C.Const.pincode,
				pincode));
		nameValuePairs.add(new BasicNameValuePair(C.Const.active,
				String.valueOf(active)));
		return nameValuePairs;
	}

	public void commit(final String response) {
		JSONObject object;
		try {
			object = new JSONObject(response);
			this.id = object.getLong(C.Const.id);
			this.email = object.getString(C.Const.email);
			this.pass = object.getString(C.Const.pass);
			this.fName = object.getString(C.Const.fName);
			this.lName = object.getString(C.Const.lName);
			this.sName = object.getString(C.Const.sName);
			this.phone = object.getString(C.Const.phone);
			this.role = object.getString(C.Const.role);
			this.pincode = object.getString(C.Const.pincode);
			this.active = object.getBoolean(C.Const.active);
			commit();
		} catch (JSONException e) {
			clear();
		}
	}

	public void commit() {
		SharedPreferences.Editor editor = settings.edit();
		editor.putLong(C.Const.id, this.id);
		editor.putString(C.Const.email, this.email);
		editor.putString(C.Const.pass, this.pass);
		editor.putString(C.Const.fName, this.fName);
		editor.putString(C.Const.lName, this.lName);
		editor.putString(C.Const.sName, this.sName);
		editor.putString(C.Const.phone, this.phone);
		editor.putString(C.Const.role, this.role);
		editor.putString(C.Const.pincode, this.pincode);
		editor.putBoolean(C.Const.active, this.active);
		editor.commit();
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
		settings = context.getSharedPreferences(PREFS_NAME, 0);
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getfName() {
		return fName;
	}

	public void setfName(String fName) {
		this.fName = fName;
	}

	public String getlName() {
		return lName;
	}

	public void setlName(String lName) {
		this.lName = lName;
	}

	public String getsName() {
		return sName;
	}

	public void setsName(String sName) {
		this.sName = sName;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getPincode() {
		return pincode;
	}

	public void setPincode(String pincode) {
		this.pincode = pincode;
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	public void setPincodenew(String pincodenew) {
		this.pincodenew = pincodenew;
	}

	public String getPincodenew() {
		return pincodenew;
	}
}
